package com.example.index

import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import com.cat.annotation.IndexEntry
import com.example.index.arch.ArchIndexActivity
import com.example.index.data.room.LargeTextRoomActivity
import com.example.index.download.FileDownLoadActivity
import com.example.index.extension.quickStartActivity
import com.example.index.extension.string
import com.example.index.gesture.GestureIndexActivity
import com.example.index.reflect.TestListActivity
import com.example.index.service.RemoteEventBusActivity
import com.example.index.system.*
import com.example.index.system.fitsystemwindow.FitSystemWindowActivity
import com.example.index.system.fitsystemwindow.FitSystemWindowIndexActivity
import com.example.index.tools.ToolsIndexActivity
import com.example.index.tools.glide.GlideBase64DecoderActivity
import com.example.index.tools.glide.GlideIndexActivity
import com.example.index.ui.UiIndexActivity
import com.example.index.util.SignChecker
import es.dmoral.toasty.Toasty
import io.reactivex.Flowable
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.lang.IllegalStateException

@IndexEntry(parentIndexId = "", id = "BaseIndex")
class IndexActivity : BaseIndexActivity() {

    val TAG = "IndexActivity"

    val items = arrayListOf<Pair<String, Class<*>>>()


    init {

        items.addAll(
            arrayListOf(
                Pair(string(R.string.lang_switch), LanguageSwitchActivity::class.java),
                Pair(string(R.string.fingerprint_unlock), FingerPrintUnlockActivity::class.java),
                Pair(string(R.string.fitsystemWindow), FitSystemWindowIndexActivity::class.java),
                Pair(string(R.string.gesture_detect_index), GestureIndexActivity::class.java),
                Pair(string(R.string.ui), UiIndexActivity::class.java),
                Pair("BaseListActivity", TestListActivity::class.java),
                Pair("WebViewActivity", WebViewActivity::class.java),
                Pair("文件下载", FileDownLoadActivity::class.java),
                Pair("监测其他App 的前台使用时间", DetectAppUseTimeActivity::class.java),
                Pair("Aidl EventBus 跨进程 事件", RemoteEventBusActivity::class.java),
                Pair("小工具", ToolsIndexActivity::class.java),
                Pair("PhoneWindow 实现代理", PhoneWindowScrollingActivity::class.java),
                Pair("EditText 的软键盘代理", LargeTextRoomActivity::class.java),
                Pair("Glide相关扩展", GlideIndexActivity::class.java),
                Pair("代码结构",ArchIndexActivity::class.java)
            )

        )
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        EventBus.getDefault().register(this)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            Log.i(
                TAG,
                "onCreate: ${packageManager.getPackageInfo(
                    packageName,
                    PackageManager.GET_SIGNING_CERTIFICATES
                )!!.signingInfo!!.signingCertificateHistory[0].toCharsString()}"
            )
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    override fun getData(): List<String> = run {


        items.map { it.first }.toList()
    }

    override fun handleItemClick(index: Int, title: String) =
        quickStartActivity(items[index].second)

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onConfigurationNeedUpdate(event: UpdateLocaleEvent) {
        recreate()
    }


    override fun onResume() {
        super.onResume()

        try {
            val forName = Class.forName("com.google.android.material.appbar.AppBarLayout")
            Log.i("IndexActivity", "onResume:---appbarlayout  class  found !!!!!!!!")

        } catch (e: ClassNotFoundException) {
            Log.i("IndexActivity", "onResume:---appbarlayout  class not found ")
        }
    }
}
